package com.chang.ware.service.impl;

import cn.hutool.Hutool;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chang.common.utils.PageUtils;
import com.chang.common.utils.Query;
import com.chang.ware.entity.WareInfoEntity;
import com.chang.ware.mapper.WareInfoDao;
import com.chang.ware.service.WareInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        LambdaQueryWrapper<WareInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.hasLength(key)) {
            lambdaQueryWrapper.and(wrapper -> {
                wrapper.eq(WareInfoEntity::getId, key)
                        .or()
                        .like(WareInfoEntity::getName, key)
                        .or()
                        .like(WareInfoEntity::getAddress, key)
                        .or()
                        .eq(WareInfoEntity::getAreacode, key);
            });
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                lambdaQueryWrapper
        );

        return new PageUtils(page);
    }

    // TODO 此处获取实际运费的因素复杂，老师的逻辑是拿着地址id去获取详细地址，这不是脱了裤子放屁吗？ 为啥不建个对象直接传递再计算，
    // TODO 并且 不同商品运费肯定不一样，并且现在一般都包邮，此处逻辑我不做了，直接随机邮费得了。老师的计算更离谱，手机号最后一位当邮费

    @Override
    public BigDecimal getFare(Long addrId) {
        // 随机运费
        //return new BigDecimal(RandomUtil.randomInt(1,20));
        return new BigDecimal(5);
    }

}